<?php
namespace app\modules\admin\models;
use Yii;
use yii\web\UploadedFile;
class Upload extends \yii\db\ActiveRecord
{
    /**
     * @var UploadedFile|Null file attribute
     */
    public $img;
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['img'], 'file', 'extensions' => 'jpg, png', 'mimeTypes' => 'image/jpeg, image/png',],
        ];
    }

    public function uploadImg()
    {
        if ($this->validate()) {
            $img_path = 'uploads/' . $this->img->baseName . '.' . $this->img->extension;
            $this->img->saveAs($img_path);
            return $img_path;
        } else {
            return $this->getFirstErrors();
        }
    }
}